package com.sk.skplanet.commute.views;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sk.skplanet.commute.R;
import com.sk.skplanet.commute.actions.API_cennectingThread;
import com.sk.skplanet.commute.actions.SortingHandler;
import com.sk.skplanet.commute.controllers.DialogController;
import com.sk.skplanet.commute.controllers.PointController;
import com.sk.skplanet.commute.vo.Data;
import com.sk.skplanet.commute.vo.Route;

public class PointList extends Activity {
	TextView textview_pointlist_title;
	LinearLayout layout_pointlist;
	RelativeLayout layout_pointlist_title;
	Button button_pointlist_addlpoint;
	int routePosition;
	Route route;
	Thread api_connectingThread;
	SortingHandler sortingHandler;
	boolean api_connectingThread_run;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pointlist);

		layout_pointlist_title = (RelativeLayout) findViewById(R.id.layout_pointlist_title);
		layout_pointlist = (LinearLayout) findViewById(R.id.layout_pointlist);
		// layout_pointlist_title.setBackgroundColor(Color.parseColor(Data.layout_pointlist_titleColor));
		layout_pointlist_title.setBackgroundResource(R.drawable.category_menu_bg_sel);
		layout_pointlist.setBackgroundColor(Color.parseColor(Data.layout_pointColor));

		textview_pointlist_title = (TextView) findViewById(R.id.textview_pointlist_title);

		routePosition = getIntent().getIntExtra("routePosition", -1);
		route = (Route) Data.memberData.getRoutes().get(routePosition);
		// Log.i("포인트리스트 접근", route.toString());
		if (routePosition != -1) {
			textview_pointlist_title.setText(route.getName());
			// textview_pointlist_title.setTextColor(Color.parseColor(Data.textview_pointColor));
			textview_pointlist_title.setTextColor(Color.parseColor("#FEFEFE"));

			if (route.getPoints() != null) {
				for (int pointPosition = 0; pointPosition < route.getPoints().size(); pointPosition++)
					layout_pointlist.addView(PointController.makePoint(this, routePosition, pointPosition));
			}
		}

		button_pointlist_addlpoint = (Button) findViewById(R.id.button_pointlist_addlpoint);

		button_pointlist_addlpoint.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DialogController.createPointDialog(PointList.this, routePosition).show();
			}
		});

		sortingHandler = new SortingHandler();

		api_connectingThread_run = true;
		api_connectingThread = new API_cennectingThread(PointList.this, layout_pointlist, route,
				api_connectingThread_run, sortingHandler);

		api_connectingThread.start();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.favorite_menu, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		api_connectingThread.interrupt();
		api_connectingThread_run = false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			api_connectingThread_run = false;
			Intent intentRouteList = new Intent(PointList.this, RouteList.class);
			startActivity(intentRouteList);
			finish();
		}
		return false;
		// return super.onKeyDown(keyCode, event);
	}

}
